

import * as Cesium from "cesium";

/**
 * @description 获取UTC时间
 * @returns {String} 2025-01-12 00:00:00
 */
export const getUTCTime = () => {
    let now = new Date();
    let mounth = (now.getMonth() + 1) < 10 ? '0' + (now.getMonth() + 1) : (now.getMonth() + 1);
    let day = now.getDate() < 10 ? '0' + now.getDate() : now.getDate();
    let hour = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();
    let minute = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
    let second = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();
    return now.getFullYear() + '-' + mounth + '-' + day + " " + hour + ':' + minute + ':' + second;
}
export const getHeading = (pointA, pointB) => {
    const transform = Cesium.Transforms.eastNorthUpToFixedFrame(pointA)
    const positionvector = Cesium.Cartesian3.subtract(pointB, pointA, new Cesium.Cartesian3())
    const vector = Cesium.Matrix4.multiplyByPointAsVector(Cesium.Matrix4.inverse(transform, new Cesium.Matrix4()), positionvector, new Cesium.Cartesian3())
    const direction = Cesium.Cartesian3.normalize(vector, new Cesium.Cartesian3())
    const heading = Math.atan2(direction.y, direction.x) - Cesium.Math.PI_OVER_TWO
    return Cesium.Math.TWO_PI - Cesium.Math.zeroToTwoPi(heading)
}

// 计算俯仰角
export const getPitch = (pointA, pointB) => {
    const transfrom = Cesium.Transforms.eastNorthUpToFixedFrame(pointA)
    const vector = Cesium.Cartesian3.subtract(pointB, pointA, new Cesium.Cartesian3())
    const direction = Cesium.Matrix4.multiplyByPointAsVector(Cesium.Matrix4.inverse(transfrom, transfrom), vector, vector)
    Cesium.Cartesian3.normalize(direction, direction)
    return Cesium.Math.PI_OVER_TWO - Cesium.Math.acosClamped(direction.z)
}
export const saveToImage = ()  => {
    window.miniToEnemyViewer.render()
    let canvas = window.miniToEnemyViewer.scene.canvas
    let image = canvas.toDataURL('image/png').replace('image/png', 'image/octet-stream')
    let link = document.createElement('a')
    let blob = dataURLtoBlob(image)
    let objurl = URL.createObjectURL(blob)
    link.download = 'scene.png'
    link.href = objurl
    link.click()

    function dataURLtoBlob(dataurl) {
        let arr = dataurl.split(','),
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]),
            n = bstr.length,
            u8arr = new Uint8Array(n)
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n)
        }
        return new Blob([u8arr], {
            type: mime
        })
    }
}
